/**
 * @(#) PrevJobPlaceProxy.as
 *
 * @date		2011. 01. 07.
 * @version		2.0
 * @author		Shin, Yunwoo
 * @description	 연말정산  - Proxy
 * 				종전근무지 Proxy
 */
package com.skt.common_hr.yearadjustment.model
{
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.skt.common_hr.yearadjustment.model.vo.PrevJobPlaceDetailVO;
	import com.skt.common_hr.yearadjustment.model.vo.PrevJobPlaceVO;
	import com.wcl.dataloader.*;
	import com.wcl.utils.DateUtil;
	import com.wcl.utils.Hourglass;
	
	import mx.collections.ArrayCollection;
	import mx.managers.CursorManager;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;

	public class PrevJobPlaceProxy extends CommonProxy
	{
		public static const NAME:String = "PrevJobPlaceProxy";
		
		public static const LOAD_PREV_JOBPLACE_LIST_COMPLETE:String = "loadPrevJobPlaceListComplete";
		public static const LOAD_PREV_JOBPLACE_LIST_FAILED:String = "loadPrevJobPlaceListFailed";
		public static const SAVE_PREV_JOBPLACE_LIST_COMPLETE:String = "savePrevJobPlaceListComplete";
		public static const SAVE_PREV_JOBPLACE_LIST_FAILED:String = "savePrevJobPlaceListFailed";
		
		public static const LOAD_DATA_TYPES_COMPLETE:String = "loadPrevJobPlaceDataTypeComplete";
		public static const LOAD_DATA_TYPES_FAILED:String = "loadPrevJobPlaceDataTypeFailed";
		
		public var msgStr:String;
		
		public var prevJobPlaceList:ArrayCollection;
		public var dataTypes:ArrayCollection;

		/**
		 * 생성자
		 * 
		 * @param
		 * @return void
		 */
		public function PrevJobPlaceProxy(data:Object=null)
		{
			super(NAME, data);
		}
		
		
		/**
		 * data load를 수행
		 * 		- DataLoader 를 생성하고 Import Parameter 객체를 생성, RFC를 호출
		 * 
		 * @param pernr 사번
		 * @return void
		 */
		public function load(pernr:String, year:String):void
		{
			//var loader:IDataLoader = DataLoaderFactory.getDataLoader("Z_HR_0083");
			
			var param:Object = new Object();
			param["I_PERNR"] = pernr;
			param["I_YEAR"] = year;
			param["I_PGBN"] = "1";
			loadData("Z_HR_0083_NEW", param, result, fault); 
			//PsHrAlert.showMessage( "load : " + param["I_PERNR"] + ", " + param["I_YEAR"] + ", " + param["I_PGBN"]);
		}
		
		public function loadDetail():void
		{
			var params:Object = new Object();
			loadData("Z_HR_0301", params, resultTypes, faultTypes);
		}
		
		public function save(pernr:String, year:String, head:Array, item:Array):void
		{
			Hourglass.show(null, "데이터를 저장 중입니다.", "저장");

			var param:Object = new Object();
			param["I_PERNR"] = pernr;
			param["I_YEAR"] = year;
			param["I_PGBN"] = "2";

			var tables:Object  = new Object();
			tables["I_HEAD"] = head;
			tables["I_ITEM"] = item;

			saveData("Z_HR_0083_NEW", param, tables, saveResult, saveFault); 
		}
		
		
		
		
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event Listeners
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 
		 * @param
		 * @return void
		 */
		public function result(event:ResultEvent):void
		{
			if(retCode==0) {  
				prevJobPlaceList = parseJobPlaceList(event.result);
				facade.sendNotification(LOAD_PREV_JOBPLACE_LIST_COMPLETE, this);
			}
			else {
				msgStr = event.result["RETTEXT"];
				facade.sendNotification(LOAD_PREV_JOBPLACE_LIST_FAILED, this);
			}
		}
		
		public function resultTypes(event:ResultEvent):void
		{
			if(retCode==0) {  
				dataTypes = parseDataTypes(event.result.I_LGA);
				facade.sendNotification(LOAD_DATA_TYPES_COMPLETE, this);
			}
			else {
				msgStr = event.result["RETTEXT"];
				facade.sendNotification(LOAD_DATA_TYPES_FAILED, this);
			}
		}


		public function saveResult(event:ResultEvent):void
		{
			Hourglass.remove();
			if(event.result["RETCODE"]==0) {  
				facade.sendNotification(SAVE_PREV_JOBPLACE_LIST_COMPLETE, this);
				CursorManager.removeBusyCursor();
			}
			else {
				trace( "failed to load totalIncom: (" +event.result["RETCODE"] + ") "+event.result["RETTEXT"]);
				msgStr = event.result["RETTEXT"];
				facade.sendNotification(SAVE_PREV_JOBPLACE_LIST_FAILED, this);
				CursorManager.removeBusyCursor();
			}
		}
		
		/**
		 * 서버에서 오류가 발생했을 처리
		 * 
		 * @param
		 * @return void
		 */
		private function fault(event:FaultEvent):void
		{
			msgStr = event.fault.faultString;
			facade.sendNotification(LOAD_PREV_JOBPLACE_LIST_FAILED, this);
		}
		
		/**
		 * 서버에서 오류가 발생했을 처리
		 * 
		 * @param
		 * @return void
		 */
		private function faultTypes(event:FaultEvent):void
		{
			msgStr = event.fault.faultString;
			facade.sendNotification(LOAD_PREV_JOBPLACE_LIST_FAILED, this);
		}

		/**
		 * 서버에서 오류가 발생했을 처리
		 * 
		 * @param
		 * @return void
		 */
		private function saveFault(event:FaultEvent):void
		{
			Hourglass.remove();
			msgStr = event.fault.faultString;
			facade.sendNotification(SAVE_PREV_JOBPLACE_LIST_FAILED, this);
		}
		
		
		
		
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Data Parsing functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 전달된 result 객체를 분석하여 Value Object를 생성
		 * 
		 * @param
		 * @return 연말정산  MainList Value Object
		 */
		private function parseDataTypes(arr:ArrayCollection):ArrayCollection
		{
			var returnArr:ArrayCollection = new ArrayCollection();
			for each(var reg:Object in arr){
				var vo:PrevJobPlaceDetailVO = new PrevJobPlaceDetailVO();
				
				vo.SEQNR	= reg["SEQNR"];
				vo.SEQNO	= reg["SEQNO"];	//항목명
				vo.LGA		= reg["LGA"];	//임금 유형
				vo.LGAT		= reg["LGAT"];	//임금 유형 설명
				vo.BET		= reg["BET"];	//금액
				returnArr.addItem(vo);
			}
			return returnArr;
		}
		
		private function parseJobPlaceList(RESULT:Object):ArrayCollection
		{
			var returnArr:ArrayCollection = new ArrayCollection();
			var no:int=1;
			
			for each(var place:Object in RESULT.I_HEAD){
				var vo:PrevJobPlaceVO = new PrevJobPlaceVO();
				vo.no 		= no++;
				vo.SEQNR	= place["SEQNR"];
				vo.BIZNO	= place["BIZNO"];	//사업자번호	
				vo.COMNM	= place["COMNM"];	//회사이름	
				vo.TXPAS	= place["TXPAS"]; //납세조합 구분
				vo.PABEG	= DateUtil.stringToDateTime(place["PABEG"]); //근무시작일자
				vo.PAEND	= DateUtil.stringToDateTime(place["PAEND"]); //근무종료일자
				vo.EXBEG	= DateUtil.stringToDateTime(place["EXBEG"]); //감면시작일자
				vo.EXEND	= DateUtil.stringToDateTime(place["EXEND"]); //감면종료일자
				
				vo.detail = new ArrayCollection();
				for each(var type:Object in RESULT.I_ITEM) {
					if(type.SEQNR == vo.SEQNR) {
						var typeVO:PrevJobPlaceDetailVO = new PrevJobPlaceDetailVO;
						typeVO.SEQNR = type.SEQNR;
						typeVO.SEQNO = type.SEQNO;
						typeVO.LGA = type.LGA;
						typeVO.LGAT = type.LGAT;
						typeVO.BET = type.BET;
						vo.detail.addItem(typeVO);
					}
				}
				returnArr.addItem(vo);
			}
			
			return returnArr;
		}
		
	}
}